import HttpRequestAPI from '@/services'
import type { UserInfo } from '@/stores/home'
//TS 类型声明

/**
 * 登录
 */
interface LoginUserDto {
  email: string
  password: string
  checkCode: string
  checkCodeKey: string
}
/**
 * 用户信息
 */
interface TokenUserInfoDto {
  userId: string
  nickName: string
  avatar: string
  // token过期时间戳，单位：毫秒
  expireTime: number
  token: string
  fansCount: number
  currentCoinCount: number
  focusCount: number
}
/**
 * 注册
 */
export interface UserInfoDto {
  nickName: string
  email: string
  password: string
  checkCode: string
  checkCodeKey: string
}
/**
 * 用户的硬币数 粉丝数 关注数等信息
 */
export interface UserCountInfoDto {
  fansCount: number
  focusCount: number
  currentCoinCount: number
}
/**
 * 首页推荐视频列表
 */
export interface VideoInfo {
  videoId: string
  nickName: string
  avatar: string
  categoryId: number
  coinCount: number
  collectCount: number
  commentCount: number
  createTime: string
  damnuCount: number
  duration: number
  interaction: string
  introduction: string
  lastPlayTime: string
  lastUpdateTime: string
  likeCount: number
  originInfo: string | null
  pcategoryId: number
  playCount: number
  postType: number
  recommendType: number
  tags: string
  userId: string
  videoCover: string
  videoName: string
}
/**
 * 首页视频列表
 */
export interface VideoInfoPaginationResponse {
  totalCount: number
  pageSize: number
  pageNo: number
  pageTotal: number
  list: VideoInfo[]
}
/**
 * 系统设置类型
 */
export interface SysSettingVO {
  registerCoinCount: number
  postVideoCoinCount: number
  videoSize: number
  videoPCount: number
  videoCount: number
  commentCount: number
  danmuCount: number
}

/**
 *
 * @param 获取验证码接口
 * @returns
 */
export const getCodeApi = () => {
  return HttpRequestAPI.get<{
    checkCode: string
    checkCodeKey: string
  }>({
    url: '/web/account/checkCode',
    data: { showLoading: true }
  })
}

/**
 *
 * @param data 登录接口信息
 * @returns
 */
export const loginHttpRequest = (data: LoginUserDto) => {
  return HttpRequestAPI.post<TokenUserInfoDto>({
    url: '/web/account/login',
    data
  })
}

/**
 *
 * @param data 注册接口
 * @returns
 */
export const registerHttpRequest = (data: UserInfoDto) => {
  return HttpRequestAPI.post<UserInfo>({
    url: '/web/account/register',
    data: { showLoading: true, ...data }
  })
}

/**
 * 获取用户的硬币数 粉丝数 关注数等信息
 * @returns
 */
export const getUserCountInfo = () => {
  return HttpRequestAPI.get<UserCountInfoDto>({
    url: '/web/account/getUserCountInfo'
  })
}

/**
 * 自动登录
 * @returns
 */
export const autoLoginHttpRequest = () => {
  return HttpRequestAPI.get<UserInfo>({
    url: '/web/account/autoLogin'
  })
}

/**
 * 退出登录
 * @returns
 */
export const logoutLoginHttpRequest = () => {
  return HttpRequestAPI.get<UserInfo>({
    url: '/web/account/logout'
  })
}

/**
 * 获取推荐视频列表
 * @returns
 */
export const recommendVideoHttpRequest = () => {
  return HttpRequestAPI.get<VideoInfo[]>({
    url: '/web/video/loadRecommendVideo'
  })
}

/**
 * 获取首页视频列表
 * @returns VideoInfoPaginationResponse
 */
export const loadVideoListHttpRequest = (data: {
  categoryId?: number
  pCategoryId?: number
  pageNo?: number | string
  pageSize?: number | string
}) => {
  const formData = new FormData()
  formData.append('categoryId', String(data.categoryId))
  formData.append('pCategoryId', String(data.pCategoryId))
  formData.append('pageNo', String(data.pageNo))
  formData.append('pageSize', String(data.pageSize || ''))
  return HttpRequestAPI.post<VideoInfoPaginationResponse>({
    url: '/web/video/loadVideoList',
    data: formData
  })
}

/**
 * 获取系统设置信息
 * @returns
 */
export const sysSettingHttpRequest = () => {
  return HttpRequestAPI.get<SysSettingVO>({
    url: '/web/sysSetting/getSysSetting'
  })
}
